<?php
/**
 * Created by PhpStorm.
 * User: coolinmind
 * Date: 25/01/2019
 * Time: 17:00
 */

namespace App\Services\UCenter\UCenter;

use App\Models\Cargo\WayBill;
use App\Models\UCenter\Complain;
use Illuminate\Support\Facades\DB;
use App\Services\Setting\SettingIndex;


class ComplainLogic
{
    /**
     * 获取举报列表
     * @param $data
     * @return array
     */
    public function getComplainList($data)
    {
        if(!empty($data['number'])){
            $complain = Complain::where('number','=',$data['number'])->paginate(15);
        }
        if(!empty($data['phone'])){
            $phone = $data['phone'];
            $duser = Complain::with('waybill')->whereHas('waybill', function ($query) use ($phone) {
                $query->where('phone', '=', $phone);
            });
            $ouser = Complain::with('ouser')->whereHas('ouser', function ($query) use ($phone) {
                $query->where('user.phone', '=', $phone);
            });
            if($duser->count()!=0){
                $complain = $duser->paginate(15);
            }elseif($ouser->count()!=0){
                $complain = $ouser->paginate(15);
            }
        }
        if(empty($data['number']) && empty($data['phone'])){
            $complain = Complain::paginate(15);
        }
        return auto_success($complain,'获取成功');
    }

    public function editComplain($data){
        $deposit = SettingIndex::SettingCategory()->getSelection(['action' => 'deposit']);
        if($data['owner_cost']>$deposit['ownerDeposit'][0]){
            return auto_failure($deposit,'扣除金额不能大于保证金金额');
        }
        if($data['driver_cost']>$deposit['driverDeposit'][0]){
            return auto_failure($deposit,'扣除金额不能大于保证金金额');
        }
        $complain = Complain::where('id','=',$data['id'])->first();
        $res = Complain::where('id','=',$data['id'])->update(['reply'=>$data['reply'],'result'=>$data['result'],'reply_status'=>1,'driver_cost'=>$data['driver_cost'],'owner_cost'=>$data['owner_cost']]);
        if($res){
            WayBill::where('number','=',$complain->number)->update(['status'=>6]);
            return auto_success($res,'保存成功');
        }else{
            return auto_failure($res,'保存失败');
        }
    }


}
